package com.compass.doctor.ui.patient.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.LayoutAppendTagItemBinding
import com.compass.doctor.databinding.LayoutInspectionRecordItemBinding
import com.compass.doctor.databinding.LayoutInspectionReportItemBinding
import com.compass.doctor.databinding.LayoutInspectionReportNuclearBinding
import com.compass.doctor.databinding.LayoutUsefulExpressionsItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseMultiItemAdapter
import com.compass.framework.adapter.BaseRecyclerViewAdapter
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.saveAs
import com.compass.framework.ext.visible
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.model.InspectionRecord
import com.compass.framework.model.InspectionReport
import com.compass.framework.utils.DateUtils

/**
 * Description： 报告单adapter
 * Date:2024/3/26 11:22
 */
class InspectionReportAdapter(private var itemType: Int) :
    BaseMultiItemAdapter<InspectionReport>() {

    private lateinit var onCustomItemClick: OnCustomButtonClick


    fun setOnCustomItemClick(onCustomItemClick: OnCustomButtonClick) {
        this.onCustomItemClick = onCustomItemClick
    }


    //用来实现多item布局
    override fun getDefItemViewType(position: Int): Int {
        return itemType
    }


    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ViewBinding>,
        item: InspectionReport?,
        position: Int
    ) {
        when (holder.itemViewType) {
            4 -> {
                val binding = holder.binding.saveAs<LayoutInspectionReportNuclearBinding>()
                binding.apply {

                    orderId.text = "申请单号: ${item?.ACCESSIONNUMBER}"
                    patientId.text = "病  历  号: ${item?.PATIENTID}"
                    patientName.text = "患者姓名: ${item?.PATIENTNAMEC}"
                    patientSex.text = "患者性别: ${sexString(item?.PATIENTSEX)}"
                    studyDate.text = "检查事件: ${item?.STUDYDATE}"
                    writeDateTime.text = "检查事件: ${item?.WRITEDATETIME}"
                    doctorName.text = "报告医生: ${item?.REFERRINGPHYSICIAN}"
                    jianChaSuoJIan.text = "报告医生: ${item?.JIANCHASUOJIAN}"
                    JianChaJieLun.text = "报告医生: ${item?.JIANCHAJIELUN}"

                }

            }

            else -> {
                val binding = holder.binding.saveAs<LayoutInspectionReportItemBinding>()
                binding.apply {
                    tvDirection.text = item?.reportName
                    tvOrderId.text = item?.reportNumber
                    tvCheckTime.text =
                        DateUtils.conversionTime(item?.checkTime, DateUtils.FORMAT_TWO)


                    if (isShowCloudFilm(item?.reportName)) tvFilm.visible() else tvFilm.gone()
                    tvFilm.onClick {
                        onCustomItemClick.onButtonCallBack(position, "film")
                    }

                }
            }
        }
    }


    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return when (viewType) {
            4 -> {
                LayoutInspectionReportNuclearBinding.inflate(layoutInflater, parent, false)

            }

            else -> {
                LayoutInspectionReportItemBinding.inflate(layoutInflater, parent, false)
            }
        }

    }


    private fun isShowCloudFilm(reportName: String?): Boolean {
        reportName?.let {
            return (it.contains("数字化摄影（DR）") || it.contains("CT") || it.contains(
                "1.5T MR"
            )
                    || it.contains("3.0T MR") || it.contains("数字胃肠") || it.contains(
                "乳腺X线摄影"
            ))
        } ?: kotlin.run {
            return false
        }

    }

    private fun sexString(patientSex: String?): String {
        patientSex?.let {
            return if (it == "F") {
                "女"
            } else {
                "男"
            }
        } ?: kotlin.run {
            return "未知"
        }

    }
}